package com.sgb.kjwl.viewmodel.self;

import android.widget.Toast;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.UserAuthMsgEntity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.service.UserAPI;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.TeamComNumEntity;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelperNew;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Description
 * @Author zhaotao
 * @Date 2019/8/7
 **/
public class AuthModel3 extends BaseViewModel {

    private final AuthActivity3 activity3;

    public AuthModel3(AuthActivity3 activity3) {
        this.activity3 = activity3;
    }

    ////onResume 时 重新传入activity 创建时的activity3有可能已经销毁 导致bug
    public void getUserInfo(AuthActivity3 activity) {
        if (!activity.isFinishing()) {
            DialogHelperNew.showProgressDialog(activity).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getUserAPI().getAuthenticationUserInfo().enqueue(new Callback<BaseEntity<UserAuthMsgEntity>>() {
                @Override
                public void onResponse(Call<BaseEntity<UserAuthMsgEntity>> call, Response<BaseEntity<UserAuthMsgEntity>> response) {
                    if (response.body() == null) {
                        return;
                    }
                    if (response.body().getData() == null) return;
                    if (response.body().getState().equals("ok")) {
                        activity.showAuth(response.body().getData());
                        countMyTeamNum(response.body().getData().getNo(), activity);
                        countMyOrganizationNum(activity);
                    } else {
                        Toast.makeText(activity, response.body().getMsg(), Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<UserAuthMsgEntity>> call, Throwable t) {
                    DialogHelperNew.dismissProgressDialog();
                }
            });
        }
    }

    public void countMyTeamNum(String no, AuthActivity3 activity) {
        APIManager.getInstance().getUserAPI().countMyTeamNum(no).enqueue(new Callback<BaseEntity<TeamComNumEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TeamComNumEntity>> call, Response<BaseEntity<TeamComNumEntity>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.setTeamNumber(response.body().getData());
                } else {
                    Toast.makeText(activity, response.body().getMsg(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TeamComNumEntity>> call, Throwable t) {
            }
        });
    }

    //获取组织认证数量
    public void countMyOrganizationNum(AuthActivity3 activity){
        APIManager.getInstance().getUserAPI().countMyOrganizationNum(UserAPI.UserCenterUrl.URL_COUNT_ORGANIZATION_NUM).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data != null){
                    activity.setOrganizationNumber(data.getData());
                }
            }
        });
    }
}
